package cn.doit.day04

import scala.io.Source

object WordCount {
  def main(args: Array[String]): Unit = {
    //放的是一行一行的值
    /**
     * List("hello,hi,hello",
            "hello,hi,hello",
            "hello,hi,hello",
            "hello,hi,hello",
            "hello,hi,hello",
            "hello,hi,hello"
     )
     */
    val list: List[String] = Source.fromFile("data/word.txt").getLines().toList

    //List("hello","hi","hello","hello","hi","hello","hello","hi","hello","hello","hi","hello","hello","hi","hello")
    val words: List[String] = list.flatMap(_.split(","))

    //对list进行分组
    val map: Map[String, List[String]] = words.groupBy(word => word)

    //hello，5
    val res: Map[String, Int] = map.map(tp => {
      val key: String = tp._1
      val value: List[String] = tp._2
      (key, value.size)
    })

    res.foreach(println)


   Source.fromFile("data/word.txt").getLines()
     .toList
     .flatMap(_.split(","))
     .groupBy(word => word)
     .map(tp => (tp._1,tp._2.size))
     .foreach(println)






  }
}
